<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/

if (!defined ('EXPLAY')) exit;

$select = false;

if (isset ($_GET['id'])) {
	$select = " user_id = '".intval ($_GET['id'])."' ";
}
elseif (isset ($_GET['cat'])) {
	$login = addslashes ($_GET['cat']);
	$select = " user_login = '".substr ($login, 0, 16)."' ";
}


if ($select !== false) {
	
	$get_user = $db->query ("SELECT * FROM ".DB_PEREFIX."_users WHERE ".$select);
	
	
	
	
	if ($db->num_rows($get_user) == 1) {
	
		$user = $db->fetch_array($get_user);
		
		past_navblock (array (
							array ('Профиль', '/users/'.$user['user_login'], 'name', 'users'),
							array ('Блог', '/users/'.$user['user_login'].'/blog/', 'none', 'none'),
			
					)
					);
		
		$file = file ($_SERVER['DOCUMENT_ROOT'].'/modules/auth/admin/fields_options.txt');
		$field = array ();
		
		
		foreach ($file as $f) {
			$fld = explode ('|', $f);
			(isset($fld[2])) ? $field[$fld[0]] = $fld : $field[$fld[0]] = trim($fld[1]);
		}
		
		// =============================
		// 0 - name
		// 1 - status
		// 2 - type
		// 3 - value (not for all)
		// 4 - label
		// 5 - important
		
		
		CORE::set_page_title ($user['user_name'].' &mdash; '.$SITE['site_name']);
		
		$avatar = '/images/avatars/noavatar.jpg';
		$http = '';
		$icq = '';
		$friends = '';
		
		if ($field['avatar'] == 'on') if (file_exists ($_SERVER['DOCUMENT_ROOT'].'/images/avatars/'.$user['user_id'].'.jpg')) $avatar = '/images/avatars/'.$user['user_id'].'.jpg';
		if ($field['icq'] == 'on' && $user['user_icq'] != '' && $user['user_icq'] != 0) $icq = "<img src=\"http://web.icq.com/whitepages/online?icq={$user['user_icq']}&img=5\" width=\"18\" height=\"18\" align=\"absmiddle\" /> <a href=\"http://wwp.icq.com/scripts/contact.dll?msgto={$user['user_icq']}\" target=\"_blank\">{$user['user_icq']}</a>";
		($user['user_ban'] == 'off') ? $name = $user['user_name'] : $name = '<font color="red">'.$user['user_name'].' (пользователю запрещен доступ на сайт)</font>';
		if ($field['http'] == 'on' && $user['user_http'] != '') $http = "<a href=\"http://{$user['user_http']}\" target=\"_blank\">{$user['user_http']}</a>";
		
		$option = '';
		
		
		// пол
		if ($field['gender'] == 'on') {
			
			switch ($user['gender']) {
				case 0: $gender = 'неопределенный'; break;
				case 1: $gender = 'мужской'; break;
				case 2: $gender = 'женский'; break;
			}
			
		}
		
		// дата рождения
		if ($field['birthdate'] == 'on' && !empty($user['birthdate'])) {
			
			$birthdate = date ('j F Y', $user['birthdate']);
			
			$birthdate = str_replace ("January", "Января", $birthdate);
			$birthdate = str_replace ("February", "Февраля", $birthdate);
			$birthdate = str_replace ("March", "Марта", $birthdate);
			$birthdate = str_replace ("April", "Апреля", $birthdate);
			$birthdate = str_replace ("May", "Мая", $birthdate);
			$birthdate = str_replace ("June", "Июня", $birthdate);
			$birthdate = str_replace ("July", "Июля", $birthdate);
			$birthdate = str_replace ("August", "Августа", $birthdate);
			$birthdate = str_replace ("September", "Сентября", $birthdate);
			$birthdate = str_replace ("October", "Октября", $birthdate);
			$birthdate = str_replace ("November", "Ноября", $birthdate);
			$birthdate = str_replace ("December", "Декабря", $birthdate);
			
		}
		
		// FRIENDS
		if ($field['friends'] == 'on') {
			if (is_registered()) {
				
				if ($user['user_id'] != $GLOBAL_USER['user_id'] && !isset_friend($user['user_id']))
					$option = "<a href=\"/\" onclick=\"add_friend ({$user['user_id']}); return false;\" title=\"Добавить в друзья\"><img src=\"/themes/$theme/images/user_add.gif\" alt=\"\" align=\"absmiddle\" /></a>";
				elseif (isset_friend($user['user_id']))
					$option = "<a href=\"/\" onclick=\"add_friend ({$user['user_id']}); return false;\" title=\"Удалить пользователя из друзей\"><img src=\"/themes/$theme/images/user_delete.gif\" alt=\"\" align=\"absmiddle\" /></a>".
							  " <a href=\"/messages/write/?to={$user['user_id']}\" title=\"Написать персональное сообщение\"><img src=\"/themes/$theme/images/user_message.gif\" alt=\"\" align=\"absmiddle\" /></a>";
			
			}
		}
		
		
		// KARMA
		if ($field['karma'] == 'on') {
			
			$user_karma = (float) $user['user_karma'];
			
			Templater::load ('users/karma.tpl', false);
			
			Templater::set_var ('user_id', $user['user_id']);
			Templater::set_var ('votes', $user['karma_votes']);
			Templater::set_var ('karma', number_format($user_karma, 2, ',', ''));
			
			if ($user_karma >= 0) {
				if (is_registered() && $user['user_id'] != $GLOBAL_USER['user_id'] && not_voted ($user['user_id']) && my_karma() >= 0) {
					$karma = Templater::display_part ('good');
				} else {
					$karma = Templater::display_part ('cant_good');
				}
			}
			elseif ($user_karma < 0) {
				if (is_registered() && $user['user_id'] != $GLOBAL_USER['user_id'] && not_voted ($user['user_id']) && my_karma() >= 0) {
					$karma = Templater::display_part ('bad');
				} else {
					$karma = Templater::display_part ('cant_bad');
				}
			}
			
			Templater::set_var ('karma', $karma);
			$karma = Templater::display_part ('main', $karma);
		}
		
		
		// BLOGS
		
		$blogs = "<ul id=\"communities_list\">\r\n";
		
		$get = $db->query ("SELECT b.cat_name, b.cat_header FROM ".DB_PEREFIX."_blogs_users AS u
			LEFT JOIN (SELECT cat_id, cat_name, cat_header FROM ".DB_PEREFIX."_art_category) AS b ON b.cat_id = u.blog
			WHERE user = ".$user['user_id']);
		
		$blogs = '';
		
		if ($db->num_rows ($get) > 0) {
			
			$blogs = "<ul id=\"communities_list\">\r\n";
			
			while (list($bname, $btitle) = $db->fetch_row ($get)) {
				$blogs .= "<li class=\"community\"><a href=\"/articles/$bname/\">$btitle</a></li>\n";
			}
			
			$blogs .= '</ul>';
		}

				
		
		// COMMENTS (функция отключена)
		
		/*if ($filed['comments'] == 'on') {
			if ($_GET['del_com'] == 'true' && isset ($_GET['com_id'])) delete_comment ($_GET['com_id']);
			if (isset($_POST['com_body'])) {
				add_comment ($_POST['com_name'], $_POST['com_mail'], $_POST['com_body'], 'users', $id, $_POST['code']);
			}
			if (isset($_GET['delete_com']) && $GLOBAL_USER['user_id'] == $id) {
				
				$comment_id = intval($_POST['delete_com']);
				
				$del = $db->query ("DELETE FROM ".DB_PEREFIX."_comments WHERE com_id = '$comment_id'");
				
				if ($del) {
					$get = $db->query ("SELECT com_id, com_parent FROM ".DB_PEREFIX."_comments WHERE com_category = 'users' AND com_idi = '$id'");
					$array = array ();
					
					while (list($the_id, $parent) = $db->fetch_row ($get)) {
						$array[$parent][] = $the_id;
					}
					
					delete_com ($array[$comment_id], $array);
					site_message ('Цепочка комментариев удалена!');
				}
				else site_error ('Произошла некая ошибка!');
				
			}
		}*/
		
		$form .= "\r\n<table cellspacing=\"0\" cellpadding=\"4\" width=\"100%\">";
		if ($field['avatar'] == 'on') $form .= "\r\n<tr><td width=\"20%\" align=\"right\">Аватар:</td><td><img src=\"$avatar\" title=\"".$user['user_name']."\" /></td></tr>";
		if ($field['birthdate'] == 'on' && !empty($user['birthdate'])) $form .= "\r\n<tr><td width=\"20%\" align=\"right\">Дата рождения:</td><td><b>$birthdate</b></td></tr>";
		if ($field['gender'] == 'on') $form .= "\r\n<tr><td width=\"20%\" align=\"right\">Пол:</td><td><b>$gender</b></td></tr>";
		if ($field['icq'] == 'on' && $icq != '') $form .= "\r\n<tr><td width=\"20%\" align=\"right\">ICQ:</td><td>$icq</td></tr>";
		if ($field['http'] == 'on' && $http != '') $form .= "\r\n<tr><td width=\"20%\" align=\"right\">Сайт:</td><td><b>$http</b></td></tr>";
		
		$form .= "\r\n<tr><td align=\"right\">Присоединился:</td><td><b>".format_time($user['user_date'])."</b></td></tr>";
		$form .= "\r\n<tr><td align=\"right\">Последний визит:</td><td><b>".format_time($user['lastvisit'])."</b></td></tr>";
		
		// =============================
		// 0 - name
		// 1 - status
		// 2 - type
		// 3 - value (not for all)
		// 4 - label
		// 5 - important
		
		for ($i=1; $i<=9; $i++) {
			if (trim($field['ad'.$i][1]) == 'on' && $field['ad'.$i][2] != 'checkbox') {
				if ($user['additional'.$i] != '' || $user['additional'.$i] != 0) $form .= "\r\n<tr><td valign=\"top\" align=\"right\">".$field['ad'.$i][4].'</td><td valign="top"><b>'.n2br($user['additional'.$i]).'</b></td></tr>';
			}
		}
		
		//if ($field['friends'] == 'on') $form .= "\n<tr><td valign=\"top\" align=\"right\">Друзья:</td><td><ul class=\"friends_list\">$friends</ul></td></tr>";
		
		// FRIENDS
		if ($field['friends'] == 'on') {
					
			
			// Поиск друзей
			$get_friends = $db->query ("SELECT friend, usr.user_name, usr.user_login FROM ".DB_PEREFIX."_friends AS prnt
				LEFT JOIN (SELECT user_id, user_name, user_login FROM ".DB_PEREFIX."_users) AS usr ON usr.user_id = prnt.friend
				WHERE user = ".$user['user_id']);
				
			
			$cnt = $db->num_rows ($get_friends);
			
			if ($cnt > 0) {
				
				$form .= "\r\n<tr><td valign=\"top\" align=\"right\">".count_smth ($cnt, array ('друг','друга','друзей')).':</td><td><ul class="friends_list">';
				
				while (list ($f, $name, $flogin) = $db->fetch_row ($get_friends)) {
					$form .= " <li class=\"friend\"><a href=\"/users/$flogin\">$name</a></li>";
				}
				
				$form .= '</ul></td></tr>';
				
			}
			else $form .= "\n<tr><td valign=\"top\" align=\"right\">Друзья:</td><td><ul class=\"friends_list\">нет</ul></td></tr>";
			
			
			
			
			//В друзьях у
			$get_readers = $db->query ("SELECT COUNT(*) AS cnt FROM ".DB_PEREFIX."_friends WHERE friend = ".$user['user_id']);	
			$get_readers = $db->fetch_array ($get_readers);	
			$readers = $get_readers['cnt'];
			
			if ($readers > 0)
				$readers = count_smth ($readers, array ('человека','человек','человек'));
			else $readers = 'не находится ни у кого в друзьях';
			
			
			$form .= "\n<tr><td valign=\"top\" align=\"right\">В друзьях у:</td><td><ul class=\"friends_list\"><strong>$readers</strong></ul></td></tr>";
			

		}
		
		
		
		if ($blogs != '') $form .= "\r\n<tr><td valign=\"top\" align=\"right\">Состоит в:</td><td>$blogs</td></tr>";
		
		$form .= '</table>';
		
		past_table ('Профиль '.$user['user_name'].' '.user_status($user['user_id']).' <div style="display:inline;" id="friends_list_false">'.$option.'</div>', $form);
		
		
				
		/*if ($user['allow_comments'] == 'on' && $filed['comments'] == 'on') {
			
			print '\r\n<a name="comments"></a><h5>Стена</h5>\r\n<div id="comments_div">\r\n';
			print_comments ('users', $id, "/users/$id.html");
			print '</div>';
			past_com_table ("/users/$id.html");
			
		}*/
		
		
		
		
		
	}
	else error_404();
}
else error_404();









function isset_friend ($id) {
	global $GLOBAL_USER, $db;
	
	$get = $db->query ("SELECT * FROM ".DB_PEREFIX."_friends WHERE user = ".$GLOBAL_USER['user_id']." AND friend = ".$id);
	
	if ($db->num_rows ($get) == 1) return true;	
	
	return false;
	
}

function delete_com ($arr, $array) {
	global $db;
	
	foreach ($arr as $comment) {
		$db->query ("DELETE FROM ".DB_PEREFIX."_comments WHERE com_id = '$comment'");
		if (isset($array[$comment])) delete_comment ($array[$comment], $array);
	}
}

function not_voted ($user_id) {
	global $GLOBAL_USER;
	
	$votes = explode (',', $GLOBAL_USER['my_votes']);
	
	foreach ($votes as $v) {
		if (trim($v) == $user_id) return false;
	}
	
	return true;
}
